export const addArticleCategoryManager = (editor) => {
    const { Components } = editor;

    // 栏目项组件
    Components.addType('categoryItem', {
        model: {
            defaults: {
                tagName: 'div',
                draggable: '.category-list',
                droppable: false,
                attributes: { class: 'category-item' },
                components: [
                    {
                        type: 'text',
                        content: '栏目名称',
                        attributes: { class: 'category-name' }
                    },
                    {
                        type: 'button',
                        content: '编辑',
                        attributes: { class: 'category-edit-button' },
                    },
                    {
                        type: 'button',
                        content: '删除',
                        attributes: { class: 'category-delete-button' },
                    }
                ],
                script() {
                    const itemEl = this;

                    // 绑定编辑按钮点击事件
                    itemEl.querySelector('.category-edit-button').addEventListener('click', () => {
                        const newName = prompt('请输入新的栏目名称:', itemEl.querySelector('.category-name').innerText);
                        if (newName) {
                            itemEl.querySelector('.category-name').innerText = newName;
                        }
                    });

                    // 绑定删除按钮点击事件
                    itemEl.querySelector('.category-delete-button').addEventListener('click', () => {
                        itemEl.remove();
                    });
                },
                styles: `
                    .category-item {
                        display: flex;
                        align-items: center;
                        justify-content: space-between;
                        padding: 10px 15px;
                        border: 1px solid #ddd;
                        margin-bottom: 10px;
                        background-color: #fff;
                        border-radius: 5px;
                        transition: all 0.3s ease;
                    }
                    .category-item:hover {
                        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
                    }
                    .category-name {
                        flex: 1;
                        font-size: 16px;
                    }
                    .category-edit-button,
                    .category-delete-button {
                        margin-left: 10px;
                        cursor: pointer;
                        background-color: #007bff;
                        color: white;
                        border: none;
                        padding: 5px 10px;
                        border-radius: 5px;
                        transition: background-color 0.3s;
                    }
                    .category-edit-button:hover,
                    .category-delete-button:hover {
                        background-color: #0056b3;
                    }
                `,
            }
        },
        view: {
            events: {
                // 确保点击事件在视图中正确绑定
                'click .category-edit-button': 'onEditClick',
                'click .category-delete-button': 'onDeleteClick',
            },
            onEditClick(e) {
                const newName = prompt('请输入新的栏目名称:', this.model.get('components').models[0].get('content'));
                if (newName) {
                    this.model.get('components').models[0].set('content', newName);
                }
            },
            onDeleteClick(e) {
                this.model.remove();
            }
        }
    });

    // 栏目管理组件
    Components.addType('categoryManager', {
        model: {
            defaults: {
                tagName: 'div',
                attributes: { class: 'category-manager' },
                components: [
                    {
                        type: 'text',
                        content: '栏目管理',
                        attributes: { class: 'category-manager-title' }
                    },
                    {
                        type: 'button',
                        content: '新增栏目',
                        attributes: { class: 'category-add-button' },
                    },
                    {
                        type: 'div',
                        attributes: { class: 'category-list' },
                        components: [
                            {
                                type: 'categoryItem',
                                components: [
                                    { type: 'text', content: '示例栏目1', attributes: { class: 'category-name' } },
                                    { type: 'button', content: '编辑', attributes: { class: 'category-edit-button' } },
                                    { type: 'button', content: '删除', attributes: { class: 'category-delete-button' } }
                                ]
                            },
                            {
                                type: 'categoryItem',
                                components: [
                                    { type: 'text', content: '示例栏目2', attributes: { class: 'category-name' } },
                                    { type: 'button', content: '编辑', attributes: { class: 'category-edit-button' } },
                                    { type: 'button', content: '删除', attributes: { class: 'category-delete-button' } }
                                ]
                            }
                        ]
                    }
                ],
                script() {
                    const managerEl = this;

                    // 绑定新增栏目按钮点击事件
                    managerEl.querySelector('.category-add-button').addEventListener('click', () => {
                        const categoryName = prompt('请输入栏目名称:');
                        if (categoryName) {
                            const categoryList = managerEl.querySelector('.category-list');
                            const newCategory = document.createElement('div');
                            newCategory.className = 'category-item';
                            newCategory.innerHTML = `
                                <div class="category-name">${categoryName}</div>
                                <button class="category-edit-button">编辑</button>
                                <button class="category-delete-button">删除</button>
                            `;
                            categoryList.appendChild(newCategory);

                            // 绑定编辑和删除按钮点击事件
                            newCategory.querySelector('.category-edit-button').addEventListener('click', () => {
                                const newName = prompt('请输入新的栏目名称:', categoryName);
                                if (newName) {
                                    newCategory.querySelector('.category-name').innerText = newName;
                                }
                            });

                            newCategory.querySelector('.category-delete-button').addEventListener('click', () => {
                                newCategory.remove();
                            });
                        }
                    });
                },
                styles: `
                    .category-manager {
                        padding: 20px;
                        border: 1px solid #ddd;
                        background-color: #f9f9f9;
                        border-radius: 8px;
                        width: 80%;
                        margin: 0 auto;
                    }
                    .category-manager-title {
                        font-size: 24px;
                        margin-bottom: 20px;
                        text-align: center;
                        font-weight: bold;
                    }
                    .category-add-button {
                        display: block;
                        width: 100%;
                        cursor: pointer;
                        background-color: #28a745;
                        color: white;
                        border: none;
                        padding: 10px 20px;
                        border-radius: 5px;
                        transition: background-color 0.3s;
                        margin-bottom: 20px;
                        text-align: center;
                    }
                    .category-add-button:hover {
                        background-color: #218838;
                    }
                    .category-list {
                        display: flex;
                        flex-direction: column;
                        gap: 10px;
                    }
                    .category-item {
                        display: flex;
                        align-items: center;
                        justify-content: space-between;
                        padding: 10px 15px;
                        border: 1px solid #ddd;
                        margin-bottom: 10px;
                        background-color: #fff;
                        border-radius: 5px;
                        transition: all 0.3s ease;
                    }
                    .category-item:hover {
                        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
                    }
                    .category-name {
                        flex: 1;
                        font-size: 16px;
                    }
                    .category-edit-button,
                    .category-delete-button {
                        margin-left: 10px;
                        cursor: pointer;
                        background-color: #007bff;
                        color: white;
                        border: none;
                        padding: 5px 10px;
                        border-radius: 5px;
                        transition: background-color 0.3s;
                    }
                    .category-edit-button:hover,
                    .category-delete-button:hover {
                        background-color: #0056b3;
                    }
                `,
            }
        }
    });

    // 注册栏目管理组件
    editor.BlockManager.add('category-manager', {
        label: '栏目管理组件',
        category: '栏目类',
        content: { type: 'categoryManager' },
    });
};
